package com.yyi.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yyi.springcloud.entities.Dept;
import com.yyi.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class Dept_Hystrix_Controller {
	@Autowired
	private DeptService service;


	@RequestMapping(value = "/hystrix/dept/add", method = RequestMethod.POST)
	public boolean add(@RequestBody Dept dept)
	{
		return service.add(dept);
	}

	@RequestMapping(value = "/hystrix/dept/get/{id}", method = RequestMethod.GET)
    @HystrixCommand(fallbackMethod = "processHystrix_Get")//一旦调用服务方法失败并抛出了错误信息后，会自动调用@HystrixCommand标注好的fallbackMethod
	public Dept get(@PathVariable("id") Long id){
	    Dept dept = service.get(id);
	    if (dept == null){
	        throw new RuntimeException("该"+id+"没有对应的信息");
        }
		return dept;
	}

    public Dept processHystrix_Get(@PathVariable("id") Long id){
	    Dept dept = new Dept();
	    dept.setDeptno(id).setDname("该Id："+id+"没有对应的信息,null--@HystrixCommand").setDb_source("no this DataBase in MySql");
        return dept;
    }

	@RequestMapping(value = "/hystrix/dept/list", method = RequestMethod.GET)
	public List<Dept> list()
	{
		return service.list();
	}
}
